package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.HoChangeApplication;
import com.ruoyi.system.domain.HoDormitoryInfo;
import com.ruoyi.system.service.IHoDormitoryInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HoRepairApplicationMapper;
import com.ruoyi.system.domain.HoRepairApplication;
import com.ruoyi.system.service.IHoRepairApplicationService;
import org.springframework.util.CollectionUtils;

/**
 * 报修申请Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-10
 */
@Service
public class HoRepairApplicationServiceImpl implements IHoRepairApplicationService {
    @Autowired
    private HoRepairApplicationMapper hoRepairApplicationMapper;

    @Autowired
    private IHoDormitoryInfoService hoDormitoryInfoService;

    /**
     * 查询报修申请
     *
     * @param id 报修申请主键
     * @return 报修申请
     */
    @Override
    public HoRepairApplication selectHoRepairApplicationById(Long id) {
        return hoRepairApplicationMapper.selectHoRepairApplicationById(id);
    }

    /**
     * 查询报修申请列表
     *
     * @param hoRepairApplication 报修申请
     * @return 报修申请
     */
    @Override
    public List<HoRepairApplication> selectHoRepairApplicationList(HoRepairApplication hoRepairApplication) {
        List<HoRepairApplication> list = hoRepairApplicationMapper.selectHoRepairApplicationList(hoRepairApplication);
        if (!CollectionUtils.isEmpty(list)) {
            for (HoRepairApplication repairApplication : list) {
                if (null != repairApplication.getDormitoryId()) {
                    HoDormitoryInfo hoDormitoryInfo = new HoDormitoryInfo();
                    hoDormitoryInfo.setId(repairApplication.getDormitoryId());
                    List<HoDormitoryInfo> hoDormitoryInfos = hoDormitoryInfoService.queryHoDormitoryInfoList(hoDormitoryInfo);
                    if (!CollectionUtils.isEmpty(hoDormitoryInfos)) {
                        repairApplication.setLabel(hoDormitoryInfos.get(0).getLabel());
                    }
                }
            }
        }
        return list;
    }

    /**
     * 新增报修申请
     *
     * @param hoRepairApplication 报修申请
     * @return 结果
     */
    @Override
    public int insertHoRepairApplication(HoRepairApplication hoRepairApplication) {
        hoRepairApplication.setCreateTime(DateUtils.getNowDate());
        return hoRepairApplicationMapper.insertHoRepairApplication(hoRepairApplication);
    }

    /**
     * 修改报修申请
     *
     * @param hoRepairApplication 报修申请
     * @return 结果
     */
    @Override
    public int updateHoRepairApplication(HoRepairApplication hoRepairApplication) {
        hoRepairApplication.setUpdateTime(DateUtils.getNowDate());
        return hoRepairApplicationMapper.updateHoRepairApplication(hoRepairApplication);
    }

    /**
     * 批量删除报修申请
     *
     * @param ids 需要删除的报修申请主键
     * @return 结果
     */
    @Override
    public int deleteHoRepairApplicationByIds(Long[] ids) {
        return hoRepairApplicationMapper.deleteHoRepairApplicationByIds(ids);
    }

    /**
     * 删除报修申请信息
     *
     * @param id 报修申请主键
     * @return 结果
     */
    @Override
    public int deleteHoRepairApplicationById(Long id) {
        return hoRepairApplicationMapper.deleteHoRepairApplicationById(id);
    }
}
